package com.econ.powercloud.ems.service.biz.scene;

import com.econ.powercloud.common.base.facade.util.JacksonUtil;
import com.econ.powercloud.ems.common.enums.TableType;
import com.econ.powercloud.ems.dal.entity.ExtIotViceConfig;
import com.econ.powercloud.ems.dal.entity.ExtIotViceProduct;
import com.econ.powercloud.ems.dal.entity.ext.IotViceProductYcExt;
import com.econ.powercloud.ems.dal.entity.ext.IotViceProductYxExt;
import com.econ.powercloud.ems.pojo.scene.OpenApiSceneProductYcVO;
import com.econ.powercloud.ems.pojo.scene.OpenApiSceneProductYxVO;
import com.econ.powercloud.ems.service.dao.*;
import com.econ.powercloud.ems.util.TransformUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ Description:
 * @ Author：hgp
 * @ Create:2022-05-27-15:02
 */
@Service
public class ProductDataBizService {
    @Autowired
    private ExtIotViceProductDAO extIotViceProductDAO;
    @Autowired
    private ExtIotViceProductYcDAO extIotViceProductYcDAO;
    @Autowired
    private ExtIotViceProductYxDAO extIotViceProductYxDAO;
    @Autowired
    private ExtIotViceConfigDAO extIotViceConfigDAO;


    /**
     * 获取遥测与副表的数据
     */
    public List<OpenApiSceneProductYcVO> getYcDataByProductKey(String productKey){
        ExtIotViceProduct extIotViceProduct = extIotViceProductDAO.getProductByProductKey(productKey,null);
        Assert.notNull(extIotViceProduct, "该产品未进行配置！");
        List<IotViceProductYcExt> productYcList = extIotViceProductYcDAO.getBaseMapper().selectYcExtByProductKey(productKey);
        List<ExtIotViceConfig> viceConfigList = extIotViceConfigDAO.selectByTableIdAndApp(extIotViceProduct.getApplicationCode(), TableType.YC.getCode());
        Map<String,String> viceConfigMap = viceConfigList.stream().collect(Collectors.toMap(ExtIotViceConfig::getTableField, ExtIotViceConfig::getRealField));
        //对象转化
        List<OpenApiSceneProductYcVO> result = Lists.newArrayList();
        productYcList.stream().forEach(d->{
            OpenApiSceneProductYcVO ycVO = JacksonUtil.convertToObj(d, OpenApiSceneProductYcVO.class);
            ycVO.setIdentificationMap(TransformUtil.tramsform(d,viceConfigMap));
            result.add(ycVO);
        });
        return result;
    }

    /**
     * 获取遥信与副表的数据
     */
    public List<OpenApiSceneProductYxVO> getYxDataByProductKey(String productKey){
        ExtIotViceProduct extIotViceProduct = extIotViceProductDAO.getProductByProductKey(productKey,null);
        Assert.notNull(extIotViceProduct, "该产品未进行配置！");
        List<IotViceProductYxExt> productYxList = extIotViceProductYxDAO.getBaseMapper().selectYxExtByProductKey(productKey);
        List<ExtIotViceConfig> viceConfigList = extIotViceConfigDAO.selectByTableIdAndApp(extIotViceProduct.getApplicationCode(), TableType.YX.getCode());
        Map<String,String> viceConfigMap = viceConfigList.stream().collect(Collectors.toMap(ExtIotViceConfig::getTableField, ExtIotViceConfig::getRealField));
        //对象转化
        List<OpenApiSceneProductYxVO> result = Lists.newArrayList();
        productYxList.stream().forEach(d->{
            OpenApiSceneProductYxVO yxVO = JacksonUtil.convertToObj(d, OpenApiSceneProductYxVO.class);
            yxVO.setIdentificationMap(TransformUtil.tramsform(d,viceConfigMap));
            result.add(yxVO);
        });
        return result;
    }

}
